package tamagoshi;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class TamaWindow extends JFrame implements ActionListener {

    /**
     * 
     */
    TamaPanelGameForest fondForest;
    TamaPanelGameMars fondMars;
    TamaPanelMenu fond_menu;
    Component p1;
    JPanel fond_panel;
    Jeu jeu;
    public String nametmp;
    private static final long serialVersionUID = 1L;

    public TamaWindow(String ent, Jeu jeu) {
        super();
        this.jeu = jeu;
        build(ent);//On initialise notre fen�tre

    }

    private void build(String ent) {
        setTitle("Tamagoshi Project"); //On donne un titre � l'application
        setSize(307, 410); //On donne une taille � notre fen�tre
        setLocationRelativeTo(null); //On centre la fen�tre sur l'�cran
        setResizable(false); //On interdit la redimensionnement de la fen�tre
        menuCrea();
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //On dit � l'application de se fermer lors du clic sur la croix

        JLabel label = new JLabel();

        Component l1 = add(label);
        fond_panel = new JPanel();
        fond_panel.setLayout(new BorderLayout());
        p1 = add(fond_panel);


        runOrMenu(ent);


    }

    public void menuCrea() {
        JMenuBar menuBar = new JMenuBar();
        setJMenuBar(menuBar);
        JMenu fileMenu = new JMenu("Menu");
        JMenu helpMenu = new JMenu("Aide");
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);

        JMenuItem newAction = new JMenuItem("Nouvelle Partie");
        JMenuItem loadAction = new JMenuItem("Charger");
        JMenuItem saveAction = new JMenuItem("Sauvegarder");
        JMenuItem exitAction = new JMenuItem("Exit");
        JMenuItem aboutAction = new JMenuItem("A propos de Tamagoshi");


        ButtonGroup bg = new ButtonGroup();
        fileMenu.add(newAction);
        fileMenu.add(loadAction);
        fileMenu.add(saveAction);
        fileMenu.addSeparator();
        fileMenu.add(exitAction);
        helpMenu.add(aboutAction);


        exitAction.addActionListener(this);
        exitAction.setActionCommand("Quitter");
        newAction.addActionListener(this);
        newAction.setActionCommand("NewGame");
        saveAction.addActionListener(this);
        saveAction.setActionCommand("SaveGame");
        loadAction.addActionListener(this);
        loadAction.setActionCommand("LoadGame");
        aboutAction.addActionListener(this);
        aboutAction.setActionCommand("About");


    }

    public void runOrMenu(String game) {
        fond_panel.removeAll();

        if (game.contentEquals("Foret")) {
            this.fondForest = new TamaPanelGameForest();
            fond_panel.revalidate();
            fondForest.repaint();
            fond_panel.add(fondForest, (BorderLayout.CENTER));
            getContentPane().add(fond_panel);

        } else if (game.contentEquals("Mars")) {
            this.fondMars = new TamaPanelGameMars();
            fond_panel.revalidate();
            fondMars.repaint();
            fond_panel.add(fondMars, (BorderLayout.CENTER));
            getContentPane().add(fond_panel);


        } else if (game.contentEquals("Menu")) {
            this.fond_menu = new TamaPanelMenu();
            fond_menu.repaint();
            fond_panel.add(fond_menu, (BorderLayout.CENTER));
            getContentPane().add(fond_panel);

            JButton boutonNewGame = new JButton("Nouvelle Partie");
            boutonNewGame.setBounds(53, 200, 200, 50);

            JButton boutonCharger = new JButton("Charger");
            boutonCharger.setBounds(53, 260, 200, 50);

            fond_menu.setLayout(null);
            fond_menu.add(boutonNewGame);
            fond_menu.add(boutonCharger);

            boutonNewGame.addActionListener(this);
            boutonNewGame.setActionCommand("NewGame");
        }
    }

    @Override
    public void actionPerformed(ActionEvent contexte) {
        String command = contexte.getActionCommand();

        if (command.equals("Quitter")) {
            System.exit(0);
        } else if (command.equals("NewGame")) {
            String ent = "Menu";
            String nomTama = "";
            nomTama = JOptionPane.showInputDialog(null, "Entrez Le nom : ", "Creation tama", 1);
            System.out.println("" + nomTama);
            if (nomTama.isEmpty() || nomTama.contentEquals("") || nomTama.contentEquals("Menu") || nomTama == null) {
                JOptionPane.showMessageDialog(null, "Nom invalide ou inexistant", "Action annulée", 1);
            } else {
                ent = choixEnvironement();
                runOrMenu(ent);
                this.nametmp = nomTama;
                jeu.creaJeu(nomTama, ent);
            }
        } else if (command.equals("About")) {
            String separate = System.lineSeparator();

            ImageIcon IconAbout = new ImageIcon("src/logoAbout.png");
            JOptionPane.showMessageDialog(null, "Projet réalisé par :" + separate + "Hugo Rovelli" + separate + "Mickael Pastor" + separate + "Laurent Crépin", "About", JOptionPane.INFORMATION_MESSAGE, IconAbout);
        }


    }

    private String choixEnvironement() {
        int choix = 20;
        String ent = null;

        String menu[] = {"Mars", "Foret"};

        choix = JOptionPane.showOptionDialog(null,
                "Faites votre choix :", "ENVIRONNEMENT",
                0, JOptionPane.QUESTION_MESSAGE, null,
                menu, menu[1]);

        switch (choix) {
            case 0:
                ent = "Mars";
                break;
            case 1:
                ent = "Foret";
                break;
        }
        return ent;
    }

    public void EtatView(String etat) {
        if (etat.equals("douche")) {
            JOptionPane.showMessageDialog(null, nametmp + " est sale!!");
        } else if (etat.equals("faim")) {
            JOptionPane.showMessageDialog(null, nametmp + " a faim!!");
        } else if (etat.equals("ennui")) {
            JOptionPane.showMessageDialog(null, nametmp + " s'ennuit!!");
        } else if (etat.equals("malade")) {
            JOptionPane.showMessageDialog(null, nametmp + " est malade !!");
        }
    }

    public void action() {
        int choix = 20;
        String ent = null;

        String menu[] = {"Manger", "S'amuser", "Se doucher", "Soigner"};

        choix = JOptionPane.showOptionDialog(null,
                "Faites votre choix :", "Changement d'état :",
                0, JOptionPane.QUESTION_MESSAGE, null,
                menu, menu[3]);
        //Manger,Douche,Distraire,Soin;
        switch (choix) {
            case 0:
                jeu.Tama.Manger.setJauge();
                JOptionPane.showMessageDialog(null, nametmp + " est en rassasié !");
                break;
            case 1:
                jeu.Tama.Distraire.setJauge();
                JOptionPane.showMessageDialog(null, nametmp + " est en content !");
                break;
            case 2:
                jeu.Tama.Douche.setJauge();
                JOptionPane.showMessageDialog(null, nametmp + " est en propre comme un sous neuf !");
                break;
            case 3:
                jeu.Tama.Soin.setJauge();
                JOptionPane.showMessageDialog(null, nametmp + " est en pleine forme !");
                break;
                
        }

    }
}